package remoteControl.panel;

import java.io.IOException;
import java.io.OutputStream;
import java.util.UUID;

import android.app.Activity;
import android.bluetooth.BluetoothAdapter;
import android.bluetooth.BluetoothDevice;
import android.bluetooth.BluetoothSocket;
import android.content.Context;
import android.os.Bundle;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.GridView;
import android.widget.ImageView;
import android.widget.Toast;

public class ControlManualBotones extends Activity 
{
 BluetoothAdapter btAdapter; 
 BluetoothSocket btSocket; 
 public BluetoothDevice device;
 private OutputStream out; 
 Button sendMsgBtn;
 Button conBtn;
 String message;
	
	
 @Override
 public void onCreate(Bundle savedInstanceState) 
 {
  super.onCreate(savedInstanceState);
  setContentView(R.layout.botones);
  
  final UUID MY_UUID = UUID.fromString("00001101-0000-1000-8000-00805F9B34FB");
  final String btAdress = "00:06:66:07:AC:2A";
  btAdapter = BluetoothAdapter.getDefaultAdapter();
  device = btAdapter.getRemoteDevice(btAdress);
  
  try 
  {
   btSocket = device.createRfcommSocketToServiceRecord(MY_UUID);
   btSocket.connect();
  } 
  catch (IOException e) 
  {
   Toast.makeText(getApplicationContext(), "Could not connect socket", Toast.LENGTH_LONG).show();
  }
  
  
  
  GridView gridview = (GridView) findViewById(R.id.gridview);
  gridview.setAdapter(new ImageAdapter(this));

  gridview.setOnItemClickListener(new OnItemClickListener() 
  {
   public void onItemClick(AdapterView<?> parent, View v, int position, long id) 
   {
	 try 
   	 {
	  out = btSocket.getOutputStream();
   	 } 
   	 catch (IOException e) 
   	 {
      Toast.makeText(getApplicationContext(), "Could not listen data", Toast.LENGTH_LONG).show();
   	 }
     try 
     {
	  out.write(position);
	 } 
     catch (IOException e) 
     {
      Toast.makeText(getApplicationContext(), "Could not send data", Toast.LENGTH_LONG).show();
	 }
    }
  });
 }
    
 public class ImageAdapter extends BaseAdapter 
 {
  private Context mContext;
  
  public ImageAdapter(Context c) 
  {
   mContext = c;
  }

  public int getCount() 
  {
   return mThumbIds.length;
  }

  public Object getItem(int position) 
  {
   return null;
  }

  public long getItemId(int position) 
  {
   return 0;
  }

  // create a new ImageView for each item referenced by the Adapter
  public View getView(int position, View convertView, ViewGroup parent) 
  {
   ImageView imageView;
   if (convertView == null)
   {
    imageView = new ImageView(mContext);
    imageView.setLayoutParams(new GridView.LayoutParams(85, 85));
    imageView.setScaleType(ImageView.ScaleType.CENTER_CROP);
    imageView.setPadding(8, 8, 8, 8);
   } 
   else 
   {
    imageView = (ImageView) convertView;
   }

   imageView.setImageResource(mThumbIds[position]);
   return imageView;
  }

  // references to our images
  private Integer[] mThumbIds = 
  {
   R.drawable.leftup, R.drawable.up,
   R.drawable.rightup, R.drawable.left,
   R.drawable.stop,R.drawable.right, 
   R.drawable.leftdown,R.drawable.down, 
   R.drawable.rightdown,
  };
 }      
 
 @Override
	protected void onDestroy() {
		// TODO Auto-generated method stub
		super.onDestroy();
		try {
			btSocket.close();
			finish();
		} catch (IOException e) {
		}
	}

@Override
protected void onStop() {
	// TODO Auto-generated method stub
	super.onDestroy();
	try {
		btSocket.close();
		finish();
	} catch (IOException e) {
	}
}

 }
